import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:home_box/pages/medicine/MedicineVM/medicine_list_vm.dart';
import 'package:home_box/widgets/header.dart';

class HomeMedicine extends StatefulWidget {
  const HomeMedicine({super.key});

  @override
  State<HomeMedicine> createState() => _HomeMedicineState();
}

class _HomeMedicineState extends State<HomeMedicine>{
  @override
  void initState() {
    super.initState();
    Get.put(MedicineListVM(), tag: "MedicineListVM");
  }

  @override
  dispose(){
    Get.delete<MedicineListVM>(tag: "MedicineListVM");
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<MedicineListVM>(
      tag: "MedicineListVM",
      builder: (controller) {
        return Column(
          children: <Widget>[
            Header(showLeft: false, middleText: '药箱',middleSize: 18, rightText: '新增', showRight: true, rightCallBack: ()=>{
              Get.toNamed('/addMedicine')?.then((_) => {

              })
            }),

            ///使用Expanded展开内容，如果ListView不设置高度，就不会显示
            Expanded(
              child: ListView.builder(
                itemCount: controller.medicineList.length,
                itemBuilder: (BuildContext context, int index) {
                  return ListTile(title: Text(controller.medicines[index].name.toString()));
                },
              ),
            ),
          ],
        );
      }
    );
  }

}
